import dayjs from 'dayjs';
import { getAllInterviews, startOrEndInterview } from '@/lib/interview';
import { InterviewTimeStatus } from '@/types/interview';

// 获取面试时间状态
const getInterviewTimeStatus = (startTime: string, endTime: string): InterviewTimeStatus => {
  const now = dayjs();
  const start = dayjs(startTime);
  const end = dayjs(endTime);

  if (now.isBefore(start)) {
    return InterviewTimeStatus.NotStarted;
  } else if (now.isAfter(end)) {
    return InterviewTimeStatus.Completed;
  } else {
    return InterviewTimeStatus.InProgress;
  }
};

// 检查面试时间并自动调用相应接口
const checkInterviewTimes = async () => {
  try {
    // 获取所有面试
    const response = await getAllInterviews({
      page: 1,
      pageSize: 1000 // 获取所有面试
    });

    if (response.code === 1) {
      const interviews = response.data.row;
      const now = dayjs();

      for (const interview of interviews) {
        const startTime = dayjs(interview.startTime);
        const endTime = dayjs(interview.endTime);
        const timeStatus = getInterviewTimeStatus(interview.startTime, interview.endTime);

        // 检查是否需要开始面试
        if (now.isAfter(startTime) && timeStatus === InterviewTimeStatus.NotStarted) {
          console.log('面试自动开始:', {
            interviewId: interview.id,
            interviewName: interview.name,
            startTime: interview.startTime,
            currentTime: now.format('YYYY-MM-DD HH:mm:ss')
          });
          
          try {
            const response = await startOrEndInterview({
              interviewId: interview.id,
              startOrEnd: 1
            });
            
            if (response.code === 1) {
              console.log('面试已自动开始:', interview.name);
            } else {
              console.error('面试自动开始失败:', response.msg);
            }
          } catch (error) {
            console.error('面试自动开始出错:', error);
          }
        }

        // 检查是否需要结束面试
        if (now.isAfter(endTime) && timeStatus === InterviewTimeStatus.InProgress) {
          console.log('面试自动结束:', {
            interviewId: interview.id,
            interviewName: interview.name,
            endTime: interview.endTime,
            currentTime: now.format('YYYY-MM-DD HH:mm:ss')
          });
          
          try {
            const response = await startOrEndInterview({
              interviewId: interview.id,
              startOrEnd: 2
            });
            
            if (response.code === 1) {
              console.log('面试已自动结束:', interview.name);
            } else {
              console.error('面试自动结束失败:', response.msg);
            }
          } catch (error) {
            console.error('面试自动结束出错:', error);
          }
        }
      }
    }
  } catch (error) {
    console.error('检查面试时间失败:', error);
  }
};

// 启动定时器
let timer: NodeJS.Timeout | null = null;

export const startInterviewTimer = () => {
  if (timer) {
    clearInterval(timer);
  }

  // 立即执行一次
  checkInterviewTimes();

  // 每分钟检查一次
  timer = setInterval(checkInterviewTimes, 60000);
  console.log('面试时间检查服务已启动');
};

export const stopInterviewTimer = () => {
  if (timer) {
    clearInterval(timer);
    timer = null;
    console.log('面试时间检查服务已停止');
  }
}; 